import apiClient from '@/api/apiClient';

import { ListData, Role } from '#/entity';
import { BasicStatus } from '#/enum';

export interface RoleQueryForm {
  take: number;
  page: number;
}

export interface RoleCreateForm {
  code: string;
  name: string;
  status: BasicStatus;
  remark: string;
}

export enum RoleApi {
  List = '/security/roles',
  Role = '/security/role',
}

const getRoleList = (data: RoleQueryForm) =>
  apiClient.get<ListData<Role>>({ url: RoleApi.List, params: data });

const createRole = (data: RoleCreateForm) =>
  apiClient.post<number>({ url: RoleApi.Role, params: data });

const updateRole = (id: number, data: RoleCreateForm) =>
  apiClient.post<number>({ url: `${RoleApi.Role}/${id}`, params: data });

const deleteRole = (id: number | undefined) =>
  apiClient.post<number>({ url: `${RoleApi.Role}/${id}/delete` });

const enableRole = (data: any) => apiClient.post<number>({ url: `${RoleApi.Role}/enable`, data });

const disableRole = (data: any) =>
  apiClient.post<number>({ url: `${RoleApi.Role}/disabled`, data });

const userAddRole = (roleId: number, userId: number) =>
  apiClient.post<number>({ url: `/security/role/${roleId}/assign/${userId}` });

// /security/role/{roleId}/unassign/{userId}
const userDeleteRole = (roleId: number, userId: number) =>
  apiClient.post<number>({ url: `/security/role/${roleId}/unassign/${userId}` });

// /user/modify/password
const modifyPassword = (data: any) =>
  apiClient.post({ url: '/user/modify/password', params: data });

export default {
  getRoleList,
  createRole,
  deleteRole,
  updateRole,
  enableRole,
  disableRole,
  userAddRole,
  userDeleteRole,
  modifyPassword,
};
